import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home/index",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      {
        path: "/home/index",
        name: "/home/index",
        component: () => import("../views/Index.vue"),
      },
      {
        path: "/home/user-list", //用户列表
        name: "/home/user-list",
        component: () => import("@/views/userinfo/UserList.vue"),
        meta: {
          nav: [{ text: "用户管理" }, { text: "用户列表" }], // 动态生成导航
        },
      },
      {
        path: "/home/user-add", //添加用户
        name: "/home/user-add",
        component: () => import("@/views/userinfo/UserAdd.vue"),
        meta: {
          nav: [{ text: "用户管理" }, { text: "添加用户" }], // 动态生成导航
        },
      },
      {
        path: "/home/user-update", //修改用户信息
        name: "/home/user-update",
        component: () => import("@/views/userinfo/UserUpdate.vue"),
        meta: {
          nav: [
            { text: "用户管理" },
            { text: "用户列表", to: "/home/user-list" },
            { text: "修改用户信息" },
          ], // 动态生成导航
        },
      },
      {
        path: "/home/car-list", //车位列表
        name: "/home/car-list",
        component: () => import("@/views/carinfo/CarInfo.vue"),
        meta: {
          nav: [{ text: "车位管理" }, { text: "车位列表" }], // 动态生成导航
        },
      },
      {
        path: "/home/car-add", //新增车位
        name: "/home/car-add",
        component: () => import("@/views/carinfo/CarAdd.vue"),
        meta: {
          nav: [{ text: "车位管理" }, { text: "新增车位" }], // 动态生成导航
        },
      },
      {
        path: "/home/repair", //车位维修信息
        name: "/home/repair",
        component: () => import("@/views/carinfo/CarRepair.vue"),
        meta: {
          nav: [{ text: "车位管理" }, { text: "车位维修信息" }], // 动态生成导航
        },
      },
      {
        path: "/home/recode-info", //记录查询
        name: "/home/recode-info",
        component: () => import("@/views/inforecode/RecodeInfo.vue"),
        meta: {
          nav: [{ text: "出入记录管理" }, { text: "记录查询" }], // 动态生成导航
        },
      },
      {
        path: "/home/recode-add", //新增记录
        name: "/home/recode-add",
        component: () => import("@/views/inforecode/RecodeAdd.vue"),
        meta: {
          nav: [{ text: "出入记录管理" }, { text: "新增记录" }], // 动态生成导航
        },
      },
      {
        path: "/home/charging-info", //收费信息查询
        name: "/home/charging-info",
        component: () => import("@/views/priceinfo/ChargingInfo.vue"),
        meta: {
          nav: [{ text: "收费管理" }, { text: "收费信息查询" }], // 动态生成导航
        },
      },
      {
        path: "/home/set-price", //设置价格
        name: "/home/set-price",
        component: () => import("@/views/priceinfo/SetPrice.vue"),
        meta: {
          nav: [{ text: "收费管理" }, { text: "设置价格" }], // 动态生成导航
        },
      },
      {
        path: "/home/lots-info", //停车信息查询
        name: "/home/lots-info",
        component: () => import("@/views/lotsinfo/LotsInfo.vue"),
        meta: {
          nav: [{ text: "停车信息管理" }, { text: "停车信息查询" }], // 动态生成导航
        },
      },
      {
        path: "/home/lots-info-add", //新增停车信息
        name: "/home/lots-info-add",
        component: () => import("@/views/lotsinfo/LotsInfoAdd.vue"),
        meta: {
          nav: [{ text: "停车信息管理" }, { text: "新增停车信息" }], // 动态生成导航
        },
      },
      {
        path: "/home/myset", //管理员设置
        name: "/home/myset",
        component: () => import("@/views/myset/MySet.vue"),
        meta: {
          nav: [{ text: "管理员设置" }], // 动态生成导航
        },
      },
    ],
  },
  {
    path: "/user/login",
    name: "/user/login",
    component: () => import("../views/user/Login.vue"),
  },
  {
    path: "/404",
    name: "/404",
    component: () => import("../views/404.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

import store from "../store";
// 全局前置导航守卫每当路由跳转到目标组件之前会自动执行箭头函数
// router.beforeEach((to, from, next) => {
//   if (to.path == '/user/login') {
//     next();
//   } else {
//     if (!store.state.user) {
//       router.push('/user/login');
//     } else {
//       next();
//     }
//   }
// });

export default router;
